#!/bin/zsh
# 脚本作用：拉起或关闭商城环境
# 前置条件：已完成容器的create
# 执行方式：source quickstart.sh start | stop
# arg list:
# $1 = start : 启动中间件
# $1 = stop: 关闭kudu、impala，删除网络，deactivate 环境

if [ $1 = "start" ]
then
# 1. 启动各中间件容器
    echo "启动各中间件容器"
    echo "创建dockr 虚拟网络cdeda"
    docker network create -d bridge cdeda-network --subnet=172.20.0.0/16
    cd /Users/thyme/Documents/Development/mall4cloud/doc/中间件docker-compse一键安装/
    docker-compose -f docker-compose.yaml up -d
    cd -
    sleep 180s
    
# 2. 启动各应用服务
    echo "拉起各应用服务"
    cd /Users/thyme/Documents/Development/mall4cloud/
    # 1
    /Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home/bin/java  \
        -jar -Xms1024m -Xmx1024m -Xmn256m -Xss256k -XX:SurvivorRatio=8 -XX:+UseConcMarkSweepGC \
         mall4cloud-auth/target/mall4cloud-auth.jar &
    # 2
    /Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home/bin/java  \
        -jar -Xms1024m -Xmx1024m -Xmn256m -Xss256k -XX:SurvivorRatio=8 -XX:+UseConcMarkSweepGC \
        mall4cloud-biz/target/mall4cloud-biz.jar &
    # 3
    /Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home/bin/java  \
        -jar -Xms1024m -Xmx1024m -Xmn256m -Xss256k -XX:SurvivorRatio=8 -XX:+UseConcMarkSweepGC \
        mall4cloud-gateway/target/mall4cloud-gateway.jar &   
    # 4
    /Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home/bin/java  \
        -jar -Xms1024m -Xmx1024m -Xmn256m -Xss256k -XX:SurvivorRatio=8 -XX:+UseConcMarkSweepGC \
        mall4cloud-order/target/mall4cloud-order.jar &  
    # 5
    /Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home/bin/java  \
        -jar -Xms1024m -Xmx1024m -Xmn256m -Xss256k -XX:SurvivorRatio=8 -XX:+UseConcMarkSweepGC \
        mall4cloud-leaf/target/mall4cloud-leaf.jar &  
    # 6
    /Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home/bin/java  \
        -jar -Xms1024m -Xmx1024m -Xmn256m -Xss256k -XX:SurvivorRatio=8 -XX:+UseConcMarkSweepGC \
        mall4cloud-multishop/target/mall4cloud-multishop.jar &  
    # 7
    /Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home/bin/java  \
        -jar -Xms1024m -Xmx1024m -Xmn256m -Xss256k -XX:SurvivorRatio=8 -XX:+UseConcMarkSweepGC \
        mall4cloud-payment/target/mall4cloud-payment.jar &  
    # 8
    /Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home/bin/java  \
        -jar -Xms1024m -Xmx1024m -Xmn256m -Xss256k -XX:SurvivorRatio=8 -XX:+UseConcMarkSweepGC \
        mall4cloud-product/target/mall4cloud-product.jar &  
    # 9
    /Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home/bin/java  \
        -jar -Xms1024m -Xmx1024m -Xmn256m -Xss256k -XX:SurvivorRatio=8 -XX:+UseConcMarkSweepGC \
        mall4cloud-platform/target/mall4cloud-platform.jar &
    # 10
    /Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home/bin/java  \
        -jar -Xms1024m -Xmx1024m -Xmn256m -Xss256k -XX:SurvivorRatio=8 -XX:+UseConcMarkSweepGC \
        mall4cloud-rbac/target/mall4cloud-rbac.jar &  
    # 11
    /Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home/bin/java  \
        -jar -Xms1024m -Xmx1024m -Xmn256m -Xss256k -XX:SurvivorRatio=8 -XX:+UseConcMarkSweepGC \
        mall4cloud-search/target/mall4cloud-search.jar &  
    # 12 
    /Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home/bin/java  \
        -jar -Xms1024m -Xmx1024m -Xmn256m -Xss256k -XX:SurvivorRatio=8 -XX:+UseConcMarkSweepGC \
        mall4cloud-user/target/mall4cloud-user.jar &  
    cd -

 # 3. 启动管理前端（商家管理，也就是非最终客户访问的前端）
    echo "启动管理前端（商家管理，也就是非最终客户访问的前端）"
    cd /Users/thyme/Documents/Development/mall4cloud/front-end/mall4cloud-platform-master/
    npm run dev
    cd -
    echo "特别注意：由于Chrome浏览器缺省会执行CORB，在NPM打开之后，需执行如下内容，并将地址复制到浏览器地址栏："
    /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --args --disable-web-security --user-data-dir="/tmp/"
  #    npm install vue-cli@3.2.16 -g --force --registry=https://registry.npm.taobao.org
# 4. 启动小程序

# 4.1 启动HB，并导入mall4cloud-uniapp-master项目
# 4.2 通过HB启动"微信开发者工具"，注意需打开"设置"--> " 项目设置" --> "不校验合法域名"。

# 5. Hava fun!

elif [ $1 = "stop" ]
then 
  # 1. kill各应用服务器
  echo "kill各应用服务器"
  for var in `ps -ef |grep java |grep jar |grep mall4cloud | awk '{print $2}'`
  do 
    sudo kill -9 $var
  done
# 2. kill 各中间件容器
  echo "kill各中间件容器"
  cd /Users/thyme/Documents/Development/mall4cloud/doc/中间件docker-compse一键安装/
  docker-compose down
  cd -
# 3. kill 管理前端
  for var in `ps -ef |grep npm |grep run |grep dev | awk '{print $2}'`
    do
      sudo kill -9 $var
    done
else
    echo " arg error on"
fi
